<!DOCTYPE html>
<html>
<head>
    {include file="public/header" /}
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">


            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">题目名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="search" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <select name="status" lay-filter="searchstatus">
                                    <option value=""></option>
                                    <option value="1">已上架</option>
                                    <option value="2">已下架</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" id="formsubmit" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>


        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn  layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
{include file="public/footer" /}
<script type="text/html" id="statusTpl">
    {{#  if(d.status == 1){ }}
    <span style="color: #28f51d;">已上架</span>
    {{#  } else { }}
    <span style="color:#f42540;">已下架</span>
    {{#  } }}
</script>
<script type="text/html" id="typeTpl">
    {{#  if(d.type == 1){ }}
    <span style="">单选</span>
    {{# }else if(d.type == 2){ }}
    <span style="">多选</span>
    {{#  } }}
</script>
<script type="text/html" id="accuracyTpl">
    {{d.accuracy}}%
</script>
<script type="text/html" id="joinmemberTpl">
    {{(d.right_count+d.error_count)}}
</script>

<script>
    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate=layui.laydate;
        table.render({
            elem: '#currentTableId',
            url: '/admin/Active/question_index?action=ajax',
            method: 'post',
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data, //解析数据列表
                };
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'id', title: '序号'},
                {field: 'title', title: '题目名称'},
                {field: 'type', title: '题目类型',templet: '#typeTpl'},
                {field: 'join_member', title: '参与人数',sort:true},
                {field: 'accuracy', title: '正确率',templet: '#accuracyTpl',sort:true},
                {field: 'status', title: '活动状态',templet: '#statusTpl'},
                {title: '操作', toolbar: '#currentTableBar', minWidth: 120}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });
        form.on('select(searchstatus)',function(data){
            $("#formsubmit").click();
        })
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: data.field
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加题目',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/Active/question_add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId'),
                    data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            } else if (obj.event === 'LAYTABLE_TIPS') {
                alert('自定义的,不用的话可以将该方法删除')
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑题目',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/Active/question_write?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定要删除吗？', function (index) {
                    layer.close(index);
                    $.ajax({
                        url: "/admin/Active/question_delete",
                        type: 'post',
                        async: true,
                        data: {
                            id:data.id
                        },
                        dataType: 'json',
                        success: function (res) {
                            if(res.status){
                                parent.layer.msg("操作成功!", {icon:1,time: 1000}, function () {
                                    //重新加载父页面
                                    obj.del();

                                });
                                return;
                            }else{
                                layer.msg(res.msg);
                            }
                        }
                    });
                });
            }
        });

    });
</script>

</body>
</html>